﻿using DataLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExportImage
{
    class Program
    {
        static void Main(string[] args)
        {
            string commond = "export";
            foreach (var arg in args)
            {
                Console.WriteLine(arg);
                if (arg == "delete")
                {
                    commond = "delete";
                    continue;
                }
                if (File.Exists(arg))
                {
                    SR(arg, commond);
                }
            }
        }
        private static void SR(string path,string commond)
        {
            string filePath = Path.Combine(System.Environment.CurrentDirectory, "files");
            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            ImageManagerEntities ime = new ImageManagerEntities();
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;
            string leftStr = "";
            while ((line = sr.ReadLine()) != null)
            {
                string _line = line;
                if (!string.IsNullOrEmpty(leftStr))
                {
                    _line = leftStr + line;
                }
                if (line.Substring(line.Length - 1, 1) != ",")
                {
                    int start = line.LastIndexOf(",") + 1;
                    leftStr = line.Substring(start, line.Length - start);
                }
                string[] parts = _line.Split(',');
                List<string> list = new List<string>();
                foreach (var part in parts)
                {
                    list.Add(part);
                }
                var images = ime.ImageInfo.AsQueryable().Where(u => list.Contains(u.OriginPath)).Select(u=>u.ImagePath).ToList();
                foreach (var image in images)
                {
                    string root = GetDisk();
                    string file = Path.Combine(root, image.Substring(4));
                    if (File.Exists(file))
                    {
                        if (commond == "delete")
                        {
                            File.Delete(file);
                            Console.WriteLine(file + " deleted......ok!");
                        }
                        else
                        {
                            FileInfo fi = new FileInfo(file);
                            string filename = filePath + "\\" + Path.GetFileName(file);
                            fi.CopyTo(filename, true);
                            Console.WriteLine(fi.Name + " ......ok!");
                        }
                        
                    }
                    else
                        Console.WriteLine(file + "......not exist!");
                }
            }
        }

        private static string GetDisk()
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            string disk = "";
            foreach (DriveInfo d in allDrives)
            {
                if (d.IsReady)
                {
                    if (d.DriveType == DriveType.Removable)
                    {
                        disk = d.Name;
                        break;
                    }
                    else
                        disk = d.Name;//返回最后一个硬盘名称
                }
            }
            return disk;
        }
    }
}
